package pl.matt.media.normalizer;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import pl.matt.media.filter.EqualizeFilter;
import pl.matt.media.utils.ImageUtils;

public class ImageNormalizer {
	
	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ImageNormalizer.class);
	private String sourceDirectory = "/home/mateusz/Desktop/mateusz/priv/edukacja/mgr/praca/other_docs/BioID-FaceDatabase-V1.2.zip_FILES/";
	private String destinationDirectory = "/tmp/faces";
	private String imageExtension = ".pgm";
	
	private void normalize() {
		File directory = new File(sourceDirectory);
		String[] files = directory.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.toLowerCase().endsWith(imageExtension);
			}
		});
		for (int i = 0; i < files.length; i++) {
			try {
				String imgFileName = sourceDirectory + File.separator + files[i];
				BufferedImage srcImage = ImageUtils.readImage(imgFileName).getAsBufferedImage();
				pl.matt.media.filter.Filter filter = new EqualizeFilter();
				Image eye = filter.filter(srcImage);
				
				String dstFileName = destinationDirectory + File.separator + files[i].replaceFirst(imageExtension, ".jpeg");
				
				ImageUtils.writeImage(eye, dstFileName);
				LOG.debug(dstFileName + " <-- dstFileName saved");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	public static void main(String[] args) {
		ImageNormalizer extractor = new ImageNormalizer();
		extractor.normalize();
	}
	
}
